package com.zbkj.crmeb.user.controller;


import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.common.CommonPage;
import com.common.CommonResult;
import com.common.PageParamRequest;
import com.exception.CrmebException;
import com.utils.DateUtil;
import com.utils.ExcelUtil;
import com.zbkj.crmeb.store.model.Shop;
import com.zbkj.crmeb.store.model.excel.UserInfoExcelModel;
import com.zbkj.crmeb.store.request.ShopRequest;
import com.zbkj.crmeb.store.service.ShopService;
import com.zbkj.crmeb.user.model.User;
import com.zbkj.crmeb.user.model.UserMark;
import com.zbkj.crmeb.user.model.UserMarkEnum;
import com.zbkj.crmeb.user.model.UserRelRecord;
import com.zbkj.crmeb.user.request.UserOperateIntegralMoneyRequest;
import com.zbkj.crmeb.user.request.UserRequest;
import com.zbkj.crmeb.user.request.UserSearchRequest;
import com.zbkj.crmeb.user.request.UserUpdateSpreadRequest;
import com.zbkj.crmeb.user.response.TopDetail;
import com.zbkj.crmeb.user.response.UserResponse;
import com.zbkj.crmeb.user.service.UserMarkService;
import com.zbkj.crmeb.user.service.UserRelRecordService;
import com.zbkj.crmeb.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 用户表 前端控制器 +---------------------------------------------------------------------- | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
 * +---------------------------------------------------------------------- | Copyright (c) 2016~2020 https://www.crmeb.com All rights
 * reserved. +---------------------------------------------------------------------- | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
 * +---------------------------------------------------------------------- | Author: CRMEB Team <admin@crmeb.com>
 * +----------------------------------------------------------------------
 */
@Slf4j
@RestController
@RequestMapping("api/admin/user")
@Api(tags = "会员管理")
@Validated
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private UserMarkService userMarkService;
    @Autowired
    private UserRelRecordService userRelRecordService;
    @Autowired
    private ShopService shopService;

    /**
     * 分页显示用户表
     *
     * @param request          搜索条件
     * @param pageParamRequest 分页参数
     * @author Mr.Zhang
     * @since 2020-04-10
     */
    @ApiOperation(value = "分页列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<UserResponse>> getList(@ModelAttribute UserSearchRequest request,
                                                          @ModelAttribute PageParamRequest pageParamRequest) {
        CommonPage<UserResponse> userCommonPage = CommonPage.restPage(userService.getList(request, pageParamRequest));
        return CommonResult.success(userCommonPage);
    }

    /**
     * 删除用户表
     *
     * @param id Integer
     * @author Mr.Zhang
     * @since 2020-04-10
     */
    @ApiOperation(value = "删除")
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public CommonResult<String> delete(@RequestParam(value = "id") Integer id) {
        if (userService.removeById(id)) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation(value = "新增会员")
    @PostMapping(value = "/add")
    public CommonResult<String> add(@RequestBody UserRequest userRequest) {
        userService.saveUser(userRequest);
        return CommonResult.success();
    }

    @ApiOperation(value = "设置推荐人")
    @PostMapping(value = "/setParent")
    public CommonResult<String> setParent(@RequestBody UserRequest userRequest) {
        userRelRecordService.setParent(userRequest);
        return CommonResult.success();
    }

    /**
     * 修改用户表
     *
     * @param id          integer id
     * @param userRequest 修改参数
     * @author Mr.Zhang
     * @since 2020-04-10
     */
    @ApiOperation(value = "修改")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public CommonResult<String> update(@RequestParam Integer id, @RequestBody UserRequest userRequest) {
        userRequest.setUid(id);
        if (userService.updateUser(userRequest)) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 查询用户表信息
     *
     * @param id Integer
     * @author Mr.Zhang
     * @since 2020-04-10
     */
    @ApiOperation(value = "详情")
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public CommonResult<User> info(@RequestParam(value = "id") Integer id) {
        User user = userService.getById(id);
        return CommonResult.success(user);
    }

    /**
     * 根据参数类型查询会员对应的信息
     *
     * @param userId           Integer 会员id
     * @param type             int 类型 0=消费记录，1=积分明细，2=签到记录，3=持有优惠券，4=余额变动，5=好友关系
     * @param pageParamRequest PageParamRequest 分页
     * @author stivepeim
     * @since 2020-04-10
     */
    @ApiOperation(value = "会员详情")
    @RequestMapping(value = "/infobycondition", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", example = "1", required = true),
            @ApiImplicitParam(name = "type", value = "0=消费记录，1=积分明细，2=签到记录，3=持有优惠券，4=余额变动，5=好友关系", example = "0"
                    , required = true)
    })
    public CommonResult<CommonPage<T>> infoByCondition(@RequestParam(name = "userId") @Valid Integer userId,
                                                       @RequestParam(name = "type") @Valid @Max(5) @Min(0) int type,
                                                       @ModelAttribute PageParamRequest pageParamRequest) {
        return CommonResult.success(CommonPage.restPage((List<T>) userService.getInfoByCondition(userId, type, pageParamRequest)));
    }

    /**
     * 获取会员详情对应数据
     */
    @ApiOperation(value = "会员详情页Top数据")
    @RequestMapping(value = "topdetail", method = RequestMethod.GET)
    public CommonResult<TopDetail> topDetail(@RequestParam @Valid Integer userId) {
        return CommonResult.success(userService.getTopDetail(userId));
    }

    /**
     * 操作积分
     */
    @ApiOperation(value = "积分余额")
    @RequestMapping(value = "/operate/founds", method = RequestMethod.GET)
    public CommonResult<Object> founds(@Validated UserOperateIntegralMoneyRequest request) {
        if (userService.updateIntegralMoney(request)) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 会员分组
     *
     * @param id      String id
     * @param groupId Integer 分组Id
     * @author Mr.Zhang
     * @since 2020-04-28
     */
    @ApiOperation(value = "分组")
    @RequestMapping(value = "/group", method = RequestMethod.POST)
    public CommonResult<String> group(@RequestParam String id,
                                      @RequestParam String groupId) {
        if (userService.group(id, groupId)) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 会员标签
     *
     * @param id    String id
     * @param tagId Integer 标签id
     */
    @ApiOperation(value = "标签")
    @RequestMapping(value = "/tag", method = RequestMethod.POST)
    public CommonResult<String> tag(@RequestParam String id,
                                    @RequestParam String tagId) {
        if (userService.tag(id, tagId)) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 修改上级推广人
     */
    @ApiOperation(value = "修改上级推广人")
    @RequestMapping(value = "/update/spread", method = RequestMethod.POST)
    public CommonResult<String> editSpread(@Validated @RequestBody UserUpdateSpreadRequest request) {
        if (userService.editSpread(request)) {
            return CommonResult.success("修改成功");
        } else {
            return CommonResult.failed("修改失败");
        }
    }

    /**
     * 导入用户
     */
    @ApiOperation(value = "用户导入")
    @PostMapping("/importUser")
    public void importExcel(@RequestParam(name = "file") MultipartFile file) {
        try {
            InputStream inputStream = file.getInputStream();
            List<UserInfoExcelModel> datas = EasyExcel.read(inputStream)
                    .head(UserInfoExcelModel.class)
                    // 设置sheet,默认读取第一个
                    .sheet()
                    // 设置标题所在行数
                    .headRowNumber(1)
                    .doReadSync();
            ShopRequest shopRequest = new ShopRequest();
            shopRequest.setIsDel(0);
            Map<String, Integer> shopMap = shopService.findAll(shopRequest).stream().collect(Collectors.toMap(Shop::getName, Shop::getId));
            for (int i = 0; i < datas.size(); i++) {
                UserInfoExcelModel data = datas.get(i);
                log.info("开始导入用户，用户为：{}", JSON.toJSONString(data));
                User currentUser = userService.getByUserNo(data.getUserNo());
                // 用户已存在，跳过
                if (currentUser != null) {
                    boolean hasModify = false;
                    if (StringUtils.isNotEmpty(data.getPhone()) && !currentUser.getPhone().equals(data.getPhone())) {
                        currentUser.setPhone(data.getPhone());
                        hasModify = true;
                    }
                    if (StringUtils.isNotEmpty(data.getCardId()) && !currentUser.getCardId().equals(data.getCardId())) {
                        currentUser.setCardId(data.getCardId());
                        hasModify = true;
                    }
                    if (hasModify) {
                        userService.updateBase(currentUser);
                    }
                    log.info("用户已存在，跳过，用户为：{}", data.getUserNo());
                    continue;
                }
                /*// 不导入无手机号用户
                if (StringUtils.isBlank(data.getPhone())) {
                    log.info("用户：[{}]无手机号码，不导入此用户", data.getRealName());
                    continue;
                }*/
                User parentUser = null;
                UserMarkEnum userMarkEnum;
                // 查询用户上级
                if (StringUtils.isNotBlank(data.getParentUserNo())) {
                    parentUser = userService.getByUserNo(data.getParentUserNo());
                }
                /*// 不导入无上级用户
                if (parentUser == null) {
                    log.info("用户：[{}]无上级，不导入此用户", data.getRealName());
                    continue;
                }*/
                switch (data.getAccount()) {
                    case "健康行者":
                        userMarkEnum = UserMarkEnum.WALKER;
                        break;
                    case "健康信使":
                        userMarkEnum = UserMarkEnum.HEALTH_MSG;
                        break;
                    default:
                        userMarkEnum = UserMarkEnum.WALKER;
                        break;
                }
                // 新增用户
                User addUser = new User();
                BeanUtils.copyProperties(data, addUser);
                addUser.setAccount("");
                addUser.setNickname(data.getRealName());
                addUser.setStatus(true);
                addUser.setSpreadUid(parentUser == null ? 0 : parentUser.getUid());
                addUser.setSpreadTime(DateUtil.nowDateTime());
                addUser.setUserRankLevel(userMarkEnum.getIndex());
                addUser.setShopId(shopMap.get(data.getShopName()));
                userService.save(addUser);

                // 新增标记
                UserMark addUserMark = new UserMark();
                addUserMark.setUserId(addUser.getUid());
                addUserMark.setMarkId(userMarkEnum.getIndex());
                addUserMark.setMarkName(userMarkEnum.getName());
                addUserMark.setMarkTime(new Date());
                userMarkService.save(addUserMark);

                // 新增用户关系
                UserRelRecord addUserRelRecord = new UserRelRecord();
                addUserRelRecord.setParentId(parentUser == null ? 0 : parentUser.getUid());
                addUserRelRecord.setEffectFlag(parentUser == null ? 0 : 1);
                addUserRelRecord.setUserId(addUser.getUid());
                addUserRelRecord.setCreateTime(DateUtil.nowDateTime());
                userRelRecordService.save(addUserRelRecord);
                log.info("导入成功，用户为：{}", data.getRealName());
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new CrmebException(e.getMessage());
        }
    }
}



